package cn.tedu.csmall.order.webapi.service.impl;

import cn.tedu.csmall.cart.service.ICartService;
import cn.tedu.csmall.commons.pojo.order.dto.OrderAddDTO;
import cn.tedu.csmall.commons.pojo.order.model.OrderTb;
import cn.tedu.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.csmall.order.service.IOrderService;
import cn.tedu.csmall.order.webapi.mapper.OrderMapper;
import cn.tedu.csmall.stock.service.IStockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;

    //远程调用cart和stock
    @DubboReference
    private ICartService dubboCartService;
    @DubboReference
    private IStockService dubboStockService;

    @Override
    public void orderAdd(OrderAddDTO orderAddDTO) {
        //1.减少订单中的商品的库存(要调用stock模块)
        StockReduceCountDTO stockReduceCountDTO = new StockReduceCountDTO();
        stockReduceCountDTO.setCommodityCode(orderAddDTO.getCommodityCode());
        stockReduceCountDTO.setReduceCount(orderAddDTO.getCount());
        dubboStockService.reduceStockByCommodityCode(stockReduceCountDTO);
        //2.删除订单中勾选的商品(要调用cart模块)
        dubboCartService.deleteCartByUserIdAndCommodityCode(orderAddDTO.getUserId(),
                orderAddDTO.getCommodityCode());
        //3.新增订单
        OrderTb order = new OrderTb();
        BeanUtils.copyProperties(orderAddDTO,order);
        //执行mapper的新增方法
        orderMapper.insertOrder(order);
        log.info("订单新增成功:{}",order);
    }
}
